@isTest
private class NationalOpsCaseStatusEditControllerTest {
	
    private static final string responseString = 'Response';
    
	private static final string Role_OE = 'Order Entry';
	private static final string Role_AP = 'Ad Production Rep';
	private static final string Role_SS = 'Sales//Customer Support';
	private static final string Role_OV = 'Order Validation';
	private static final string Role_QC = 'IOG OOR - AP - Rep';
	private static final string Role_QC2 = 'IOG OOR - QC - Rep';
	private static final string Role_Billing = 'Billing';

	private static ID natoProfileId = null;
	private static Map<string,ID> natoRoleMap = null;
	private static Map<string,User> testUsers = new Map<string, User>();

    static testMethod void controllerTest() {
    	
		User u = CreateUser(Role_OV);
		System.runAs(u){

	    	Case c = new Case();
			c.RecordTypeId = RecordTypeHelper.GetRecordTypeId('Case', 'National Ops - Insertion Order');
			c.Nato_Rep_SS__c = CreateUser(Role_SS).id;   	
	    	insert c;
	    	
	        //Use the PageReference Apex class to instantiate a page
	        PageReference pageRef = Page.NationalOpsCaseStatusUpdateForm;
	        Test.setCurrentPage(pageRef);
	        ApexPages.currentPage().getParameters().put('ID', c.ID);
	        
	        ApexPages.Standardcontroller sc = new ApexPages.Standardcontroller(c);
	        NationalOpsCaseStatusEditController controller = new NationalOpsCaseStatusEditController(sc);
        
			Test.startTest();
			controller.CaseResponse = responseString;
			List<SelectOption> options =  controller.getNewStatusOptions();		
			controller.SelectedStatus = options[0].getLabel();
			controller.save();		
			Test.stopTest();
			
			List<CaseComment> comments = [Select ID, CommentBody from CaseComment where parentid = :c.ID];
			System.assertEquals(comments.size(), 1);
			System.assertEquals(comments[0].CommentBody, responseString);
	
			Case refreshedCase = [Select ID, Status from Case where ID = :c.ID][0];
			System.assertEquals(options[0].getLabel(), refreshedCase.Status);		
		}				
    }

    private static User CreateUser(string role){
    	if(!testUsers.ContainsKey(role)){
    		
	    	if(null == natoProfileId)
				natoProfileId = [Select ID from Profile where Name = 'National Operations'][0].ID;
	 
	 		if(null == natoRoleMap){
	 			natoRoleMap = new Map<string, ID>();
	 			List<string> natoRolenames = new List<string> {Role_OE,Role_AP,Role_SS,Role_OV,Role_QC,Role_QC2,Role_Billing};
				List<UserRole> roles = [Select ID, Name from UserRole where name in :natoRolenames];
				for(UserRole r: roles)
					natoRoleMap.put(r.Name, r.ID);
	 		}
	 		 
	 		ID roleID = natoRoleMap.get(role);
	 		string uniqueName = role.replace(' ', '_') + testUsers.size();
	 		
			User user = new User(alias = 'testUser', email='unitTest@attinteractive.com', 
	            emailencodingkey='UTF-8', lastname='Unit Test', languagelocalekey='en_US', 
	            localesidkey='en_US', profileid = natoProfileId, UserRoleId = roleID, 
	            timezonesidkey='America/Los_Angeles', AFrontier_Employee_ID__c= '12345' , username='unitTest@attinteractive.com.unitest.' + uniqueName);
	    
	    	insert user;
	    	testUsers.put(role, user);
    	}    	
		return testUsers.get(role);            
    }
}